package com.clouddot.baipai.ui.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.http.entity.response.getCourseTable.table;
import com.clouddot.baipai.infrastructure.environment.Env;

import java.util.List;

/**
 * Created by wxy on 2018/3/29.
 */

public class TableAdapter extends RecyclerView.Adapter<TableAdapter.MyViewHolder> {
    private Context context;
    private List<table> list;

    public TableAdapter(Context context, List<table> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (Env.getAppContext().banPaiType == 0) {
            return new MyViewHolder(LayoutInflater.from(
                    context).inflate(R.layout.h_adapter_table_item, parent,
                    false));
        } else {
            return new MyViewHolder(LayoutInflater.from(
                    context).inflate(R.layout.v_adapter_table_item, parent,
                    false));
        }

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final table table = list.get(position);
        if (table.name.contains("~")) {
            String[] s = table.name.split("~");
            holder.name.setText(s[0]);
            holder.teacheName.setText(s[1]);
            holder.teacheName.setVisibility(View.VISIBLE);
        } else {
            holder.name.setText(table.name);
            holder.teacheName.setVisibility(View.GONE);
        }

        if (table.row == 0 && table.coul == 0) {
            holder.linearLayout.setBackgroundResource(R.drawable.classtable00_item_bg);
        } else if (table.row == 0) {
            holder.linearLayout.setBackgroundResource(R.drawable.classtable0_item_bg);
        } else if (table.coul == 0) {
            if (table.row == Env.getAppContext().weekday) {
                holder.linearLayout.setBackgroundResource(R.drawable.classtable_now_item_bg);
            } else {
                holder.linearLayout.setBackgroundResource(R.drawable.classtable0_item_bg);
            }
        } else if (table.row == Env.getAppContext().weekday) {
            holder.linearLayout.setBackgroundResource(R.drawable.classtable_now_item_bg);
        } else {
            holder.linearLayout.setBackgroundResource(R.drawable.classtable_nol_item_bg);
            holder.name.setTextColor(Env.getContent().getResources().getColor(R.color.gray_3));

        }

    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView name;
        private final TextView teacheName;
        private final LinearLayout linearLayout;

        public MyViewHolder(View view) {
            super(view);
            name = view.findViewById(R.id.name);
            teacheName = view.findViewById(R.id.teacher_name);
            linearLayout = view.findViewById(R.id.linearLayout);

        }
    }

    public interface btnOnclicker {
        void editClicked(int position);
    }

    private btnOnclicker btnclicker;

    public void setbtnOnclicker(btnOnclicker btnclicker) {
        this.btnclicker = btnclicker;
    }

}

